module.exports = app => {
    return class CartService extends app.Service {
        async addCart() {
            let ctx = this.ctx;
            let app = this.app;
            let token = ctx.params.token;
            let id = ctx.params.id;
            if (!token) {
                return {
                    msg: '缺少token'
                }
            }
            if (!id || id === 'undefined') {
               return {msg: '缺少id'}
            }
            let resultSet = await app.mongo.find('foods',{query: {item_id: id}});
            delete resultSet[0]._id;
            let doc = {
                ...resultSet[0],
                id,
                buyNum: 1,
                userId: token
            }
            let insertResult = await app.mongo.insertOne('carts', {doc});
            let res = insertResult.result;
            if (res.n > 0) {
                return {
                    code: 0,
                    msg: '添加成功',
                    // 返回请求参数
                    data: {
                        id,
                        token
                    }
                }
            } else {
                return {
                    msg: '添加失败'
                }
            }
        }
        
        async delCart() {
             let ctx = this.ctx;
            let app = this.app;
            let token = ctx.params.token;
            let id = ctx.params.id;
            console.log(ctx.params);
            if (!token) {
                return {
                    msg: '缺少token'
                }
            }
            if (!id || id === 'undefined') {
               return {msg: '缺少id'}
            }
            let resultSet = await app.mongo.find('foods',{query: {item_id: id}});
            delete resultSet[0]._id;
            let doc = {
                id,
                userId: token
            }
            let result = await app.mongo.findOneAndDelete('carts', {filter: doc});
            if (result.ok == 1 || result.result.ok === 1) {
                return {
                    code: 0,
                    msg: '删除成功',
                    // 返回请求参数
                    data: {
                        id,
                        token
                    }
                }
            } else {
                return {
                    msg: '删除失败'
                }
            }
        }
        
        async clearCart() {
            let ctx = this.ctx;
            let app = this.app;
            let result = await app.mongo.deleteMany('carts',{filter: {}});
            if (result.result.ok === 1) {
                return {
                    code: 0,
                    msg: '删除成功'
                }
            } else {
                return {
                    msg: '删除失败'
                }
            }
        }

        async cartList() {
            let ctx = this.ctx;
            let app = this.app;
            let token = ctx.params.token;
            if (!token) {
                return {msg: 'no token,no data'};
            }
            let resultSet = await app.mongo.find('carts', {query: {userId: token}})
            return {
                code: 0,
                msg: 'success',
                list: resultSet
            }
        }
    }
}
